using System.IO;
using System.Text;
using BizTalk.BuildGenerator.MetaData;

namespace BizTalk.BuildGenerator.BuildFrameworks.Default.FileBuilders
{
    public class FileHelper
    {
        /// <summary>
        /// Writes the text to a file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="sb"></param>
        public static void WriteFile(string path, StringBuilder sb)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(sb.ToString());
                writer.Flush();
                fs.Close();
            }
        }
        /// <summary>
        /// Configures a file to remove the full paths and replace with parameter for source code root
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="args"></param>
        public static void ConfigureForSourceCodeRoot(StringBuilder sb, GenerationArgs args)
        {
            var directory = Path.GetDirectoryName(args.SolutionPath);
            if (directory != null) sb.Replace(directory, "$(SourceCodeRootFolder)");
        }

        /// <summary>
        /// Configures a file to set the %BTAD_InstallDir% correctly for the build script
        /// </summary>
        /// <param name="sb"></param>       
        public static void ConfigureForBizTalkInstallVariable(StringBuilder sb)
        {
            sb.Replace("%BTAD_InstallDir%", "%%BTAD_InstallDir%%");
        }        
    }
}